<template>
	<view>
		<tree :props="prop" @sendValue="confirm" :isCheck="true" :trees="tree" :yc-list="yc_list" :seled-user="sel_assist" />
	</view>
</template>

<script>
	import tree from '@/components/tree/tree.vue'
	// import { getdeptandemployNew, favoriteContacts } from '@/api/schedule/api.js'
	
	export default {
		components: {
			tree
		},
		data() {
			return {
				checkList: [],
				prop: {
					label: 'name',
					children: 'children'
				},
				tree: [],
				yc_list: [],
				sel_assist: []
			}
		},
		onLoad(option) {
			let _seledIds = option.Ids||[]
			
			this.$u.api.SelectorForSch().then(res => {
				let funSetTree = (children, autherlist) => {
					let _children = []
					for(let i = 0; i < children.length; i++) {
						let _list = ''
						if ( children[i].childmenus.length > 0 || children[i].autherlist.length > 0) {
							_list = funSetTree(children[i].childmenus, children[i].autherlist)
						}
						_children.push({
							checked: false,
							id: children[i].dept_id,
							name: children[i].dept_name,
							user: false,
							children: _list
						})
					}
					for(let i = 0; i < autherlist.length; i++) {
						let _checked = _seledIds.indexOf(autherlist[i].auther_user_id) !== -1
						let _item = {
							checked: _checked,
							id: autherlist[i].auther_user_id,
							auther_user_id: autherlist[i].auther_user_id,
							name: autherlist[i].auther_user,
							dept_name: autherlist[i].dept_name,
							user: true,
							children: ''
						}
						_children.push(_item)
						if (_checked) {
							this.sel_assist.push(_item)
						}
					}
					return _children
				}
				if (res.length) {
					for(let i = 0; i < res[0].childmenus.length; i++) {
						this.tree.push({
							checked: false,
							id: res[0].childmenus[i].dept_id,
							name: res[0].childmenus[i].dept_name,
							user: false,
							children: funSetTree(res[0].childmenus[i].childmenus, res[0].childmenus[i].autherlist)
						})
					}
				}
			}).catch(err => {
				console.error(err)
			})
			this.$u.api.favoriteContacts().then(res => {
				if (res.length > 0) {
					for(let _i = 0; _i < res.length; _i++) {
						this.yc_list.push({
							checked: _seledIds.indexOf(res[_i].auther_user_id) !== -1,
							id: res[_i].auther_user_id,
							auther_user_id: res[_i].auther_user_id,
							name: res[_i].auther_user,
							dept_name: res[_i].dept_name,
							user: true,
							children: ''
						})
					}
				}
			}).catch(err => {
				console.error(err)
			})
		},
		onBackPress() {
			
		},
		onNavigationBarButtonTap() {
			uni.$emit('assist_confirm', this.sel_assist)
			uni.navigateBack()
		},
		methods: {
			confirm(e, userlist) {
				this.sel_assist = userlist
			}
		}
	}
</script>

<style lang="less">

</style>
